package com.example.jixeishi1.controller;


import com.example.jixeishi1.entity.CartEntity;
import com.example.jixeishi1.service.CartService;
import com.example.jixeishi1.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.List;

@Controller
public class CartController {
    @Autowired
    private CartService cartService;

    /**
     * 列表
     */
    @RequestMapping("/shopping/list")
    public R list(){
        List<CartEntity> shoppingEntities = new ArrayList<>();
        shoppingEntities = cartService.getShopping();
        return R.ok().put("shopinfo",shoppingEntities);
    }

//    /**
//     * 列表
//     */
//    @RequestMapping("/shopping/orderlist")
//    public R listOrder(){
//        List<ShoppingEntity> shoppingEntities = new ArrayList<>();
//        ShoppingVo shoppingVo = new ShoppingVo();
//        Double prices =0.00;
//        shoppingEntities = shoppingService.getShopping();
//        for(ShoppingEntity shoppingEntity: shoppingEntities){
//            Double price = shoppingEntity.getPricecommodity();
//            Integer num = shoppingEntity.getNum();
//            prices += num*price;
//        }
//        shoppingVo.setPricecommodity(prices);
//        shoppingVo.setShoppingEntitys(shoppingEntities);
//        return R.ok().put("shoppingVo",shoppingVo);
//    }



    /**
     * 保存
     */
    @PostMapping("/shopping/save")
    public ModelAndView save(@RequestParam("id") Integer id){
        cartService.saveShopping(id);
        ModelAndView mav = new ModelAndView("redirect:/index");

        return mav;
    }
    /**
     * 保存
     */
    @PostMapping("/shopping/add")
    public ModelAndView saveAdd(@RequestParam("id") Integer id,@RequestParam("num") Integer num){
        cartService.addShopping(id,num);
        ModelAndView mav = new ModelAndView("redirect:/index");
        return mav;
    }
    @PostMapping("/shopping/hyphen")
    public ModelAndView hyphen(@RequestParam("id") Integer id,@RequestParam("num") Integer num){
        cartService.hyphen(id,num);
        ModelAndView mav = new ModelAndView("redirect:/index");

        return mav;
    }

    /**
     * 修改
     */
    @PostMapping("/shopping/update")
    public ModelAndView update(@RequestBody List<CartEntity> shopping){
        cartService.updateShoppingById(shopping);
        ModelAndView mav = new ModelAndView("redirect:/");
        return mav;
    }

    /**
     * 删除
     */
    @PostMapping("/shopping/delte")
    public ModelAndView update(@RequestParam("id") Integer id){
        cartService.deleteShopping(id);
        ModelAndView mav = new ModelAndView("redirect:/index");
        return mav;
    }





}
